package com.vois.jack.btmgr.classicbase;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

import com.vois.jack.btmgr.common.DeviceRecorderControlInterface;
import com.vois.jack.btmgr.util.Logger;

public class BtScoRecorder implements BtRecorderInterface {
    private Logger logger = Logger.getLogger(BtScoRecorder.class);
    private int audioSource;
    private int sampleRate;
    private boolean isRecorderStarted;
    private AudioRecord audioRecord;

    @Override
    public void create(int audioSource, int sampleRate) {
        int miniRecorderBuffer = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        this.sampleRate = sampleRate;
        try {
            if (audioSource == MediaRecorder.AudioSource.MIC) {
                this.audioSource = MediaRecorder.AudioSource.DEFAULT;
            }else {
                this.audioSource = audioSource;
            }

            audioRecord = new AudioRecord(audioSource, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, miniRecorderBuffer);
            logger.d( "create: audio created");
        }catch (IllegalArgumentException e) {
            audioRecord = null;
            e.printStackTrace();
        }
    }

    @Override
    public void release() {
        if (isRecorderStarted) {
            stopRecord();
        }

        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
    }

    @Override
    public int getAudioSource() {
        return audioSource;
    }

    @Override
    public int getSampleRate() {
        return sampleRate;
    }

    @Override
    public void startRecord() {
        if (!isRecorderStarted && audioRecord != null) {
            try {
                audioRecord.startRecording();
                isRecorderStarted = true;
            }catch (IllegalStateException e) {
                e.printStackTrace();
                if(audioRecord.getState() == AudioRecord.STATE_INITIALIZED){
                    audioRecord.stop();
                }
                audioRecord.release();
                audioRecord = null;
                isRecorderStarted = false;
            }
        }
    }

    @Override
    public void stopRecord() {
        if (isRecorderStarted && audioRecord != null) {
            try {
                audioRecord.stop();
                isRecorderStarted = false;
            }catch (IllegalStateException e) {
                e.printStackTrace();
                audioRecord.release();
                audioRecord = null;
                isRecorderStarted = false;
            }
        }
    }

    @Override
    public int putRecordData(byte[] rawData, int offset, int rawDataSize) {
        return 0;
    }

    @Override
    public int readRecordData(short[] audioData, int offsetInShort, int sizeInShort) {
        if (isRecorderStarted) {
            return audioRecord.read(audioData, offsetInShort, sizeInShort);
        }

        return 0;
    }

    @Override
    public int getAudioSessionId() {
        if (audioRecord != null) {
            return audioRecord.getAudioSessionId();
        }

        return -1;
    }

    @Override
    public int putEncodedRecordData(byte[] rawData, int offsetInByte, int rawDataSize) {
        return 0;
    }

    @Override
    public int getEncodedRecordData(byte[] rawData) {
        return 0;
    }

    @Override
    public int getRecordDataType() {
        return RAW_RECORD_DATA;
    }
}
